/**
 * 角色管理
 */
layui.config({
  base: '/'
}).extend({
  flying: 'flying'
}).define(['index', 'flying'], function (exports) {  //先加载index模块，里面配置了全局的入口
  let admin = layui.admin;
  let flying = layui.flying;
  let form = layui.form;
  let table = layui.table;
  let layer = layui.layer;
  let dropdown = layui.dropdown;

  flying.table({
    elem: '#LAY-role-table',
    url: '/role/getRolePageList', //数据接口
    cols: [
      {type: 'numbers', fixed: 'left', title: '序号'}
      , {field: 'roleId', title: '角色编码', minWidth: 100,}
      , {field: 'roleName', title: '角色名称', minWidth: 110}
      , {field: 'status', title: '状态', minWidth: 90, templet: '#statusSwitchTpl', unresize: true}
      , {field: 'updatedBy', title: '修改人', minWidth: 90}
      , {field: 'updatedTime', title: '修改时间', templet: '<div>{{ layui.util.toDateString(d.updatedTime, "yyyy-MM-dd HH:mm:ss") }}</div>',  minWidth: 170, sort: true}
      , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#role-table'}
    ]
  });

  //监听工具条
  table.on('tool(LAY-role-table)', function (obj) {
    let line_data = obj.data;

    if (obj.event === 'edit') {
      // let tr = $(obj.tr);
      layer.open({
        type: 2
        , title: '修改'
        , content: '/role/turn/edit?id=' + line_data.id
        // ,maxmin: true
        , resize: false
        , area: ['500px', '540px']
        , btn: ['确定', '取消']
        , yes: function (index, layero) {
          let iframeWindow = window['layui-layer-iframe' + index]
              , submitID = 'LAY-role-front-submit'
              , submit = layero.find('iframe').contents().find('#' + submitID);

          //监听提交
          iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
            let field = data.field; //获取提交的字段
            field.id = line_data.id;

            //提交 Ajax 成功后，更新表格中的数据
            admin.req({
              url: '/role/save'
              , type: 'post'
              , contentType: 'application/json;charset=utf-8'
              , data: JSON.stringify(field)
              , done: function (res) {
                //执行成功
                layer.msg(res.msg, {icon: 1});
                table.reload('LAY-role-table'); //数据刷新
                layer.close(index); //关闭弹层
              }
            });
          });

          submit.trigger('click');
        }
        , success: function (layero, index) {

        }
      });
    } else if (obj.event === 'more') {
      //下拉菜单
      dropdown.render({
        elem: this, // 触发事件的 DOM 对象
        show: true, // 外部事件触发即显示
        align: 'left ' , // 右对齐弹出
        data: [
          {
            title: '权限',
            id: 'perms',
            templet: '<i class="layui-icon layui-icon-list"></i> {{= d.title}}',
            href: '#'
          },
          {
            title: "删除",
            id: "del",
            templet: '<i class="layui-icon layui-icon-delete"></i> {{= d.title }}',
            href: '#'
          }
        ],
        click: function (mdata) {
          if (mdata.id === "del") {
            if ('10001' == line_data.roleId) {
              layer.msg('超级管理员角色不允许删除！', {icon: 4, time: 1000, shade: [0.3, '#000', true]});
              return;
            }

            layer.confirm('真的删除行么？', function (index) {
              //提交 Ajax 成功后，更新表格中的数据
              admin.req({
                url: '/role/del/' + line_data.id
                , type: 'post'
                , done: function (res) {
                  //执行成功
                  layer.msg(res.msg, {icon: 1});
                  table.reload('LAY-role-table'); //数据刷新
                  layer.close(index); //关闭弹层
                }
              });
            });
          } else if (mdata.id === "perms") {
            layer.open({
              type: 2
              , title: '权限分配'
              , content: '/role/turn/perms?roleId=' + line_data.roleId
              // ,maxmin: true
              , resize: false
              , area: ['500px', '540px']
              , btn: ['确定', '取消']
              , yes: function (index, layero) {
                let iframeWindow = window['layui-layer-iframe' + index]
                    , submitID = 'LAY-role-resource-front-submit'
                    , submit = layero.find('iframe').contents().find('#' + submitID);

                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                  let field = data.field; //获取提交的字段
                  let arr = [];
                  if (field.resIds) {
                    let strs = field.resIds.split(',');
                    for (let i = 0; i < strs.length; i++) {
                      let ob = {};
                      ob.roleId = field.roleId;
                      ob.resId = strs[i];

                      arr.push(ob);
                    }
                  }

                  //提交 Ajax 成功后，更新表格中的数据
                  admin.req({
                    url: '/role/perms/save'
                    , type: 'post'
                    , contentType: 'application/json;charset=utf-8'
                    , data: JSON.stringify(arr)
                    , done: function (res) {
                      //执行成功
                      layer.msg(res.msg, {icon: 1});
                      table.reload('LAY-role-table'); //数据刷新
                      layer.close(index); //关闭弹层
                    }
                  });
                });

                submit.trigger('click');
              }
              , success: function (layero, index) {

              }
            });
          }
        }
      });
    }
  });

  //对外暴露的接口
  exports('role', {});
});